See Also

Udp Class  | Udp Members  | Overload List

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Language

Visual Basic

C#

C++

C++/CLI

Show All

buffer
A byte array to be filled with the data received.
offset
Starting offset within buffer for filling.
count
Maximum number of bytes to receive.
socketFlags
A bitwise combination of special use receiving parameters.
state
User state information.
See Also Languages PowerTCP SSL Sockets for .NET

BeginReceive(Byte[],Int32,Int32,SocketFlags,Object) Method

Dart.PowerTCP.SslSockets Namespace > Udp Class > BeginReceive Method : BeginReceive(Byte[],Int32,Int32,SocketFlags,Object) Method

Asynchronously receive a datagram, specifying a buffer, offset, count and SocketFlags value.

[Visual Basic]
<DescriptionAttribute("Receive datagram into your buffer asynchronously. The EndReceive event is raised when completed.")> Overloads Public Function BeginReceive( _    ByVal buffer() As Byte, _    ByVal offset As Integer, _    ByVal count As Integer, _    ByVal socketFlags As SocketFlags, _    ByVal state As Object _ ) As IAsyncResult
[C#]
[DescriptionAttribute("Receive datagram into your buffer asynchronously. The EndReceive event is raised when completed.")] public IAsyncResult BeginReceive(    byte[] buffer,    int offset,    int count,    SocketFlags socketFlags,    object state );
[C++]
[DescriptionAttribute("Receive datagram into your buffer asynchronously. The EndReceive event is raised when completed.")] public: IAsyncResult* BeginReceive(    byte[]* buffer,    int offset,    int count,    SocketFlags socketFlags,    Object* state )
[C++/CLI]
[DescriptionAttribute("Receive datagram into your buffer asynchronously. The EndReceive event is raised when completed.")] public: IAsyncResult^ BeginReceive(    bytearray<buffer>^ buffer,    int offset,    int count,    SocketFlags socketFlags,    Object^ state )

Parameters

buffer
A byte array to be filled with the data received.
offset
Starting offset within buffer for filling.
count
Maximum number of bytes to receive.
socketFlags
A bitwise combination of special use receiving parameters.
state
User state information.

Return Type

An IAsyncResult that represents the asynchronous receive, which could still be pending.

Exceptions

ExceptionDescription
ArgumentNullExceptionbuffer is null.
ArgumentOutOfRangeExceptionoffset or count is less than 0.
ArgumentExceptionoffset + count is greater than the length of buffer.
InvalidOperationExceptionBeginXXX method used without providing an EndXXX event handler.

Remarks

Use the Receive method to asynchronously receive a datagram after first calling Open to specify a port/address to listen for datagrams. When the Udp.BeginReceive method has completed, the EndReceive event is raised. Made accessible in the EndReceive event is a Datagram object representing the datagram received. Buffer contains the actual datagram data. RemoteEndPoint contains the address/port of the remote host from which the datagram was sent.

To synchronously receive a datagram, use Receive.

A UDP datagram provides little functionality over an IP datagram, adding a port number field which allows multiplexing on the receiving host and checksum field which provides basic error handling. Unlike TCP, UDP datagrams are sent as a unit. If BeginSend is called 3 times to send 3 datagrams to a host, the receiving host will have to call Udp.BeginReceive 3 times. Also, the size of each datagram sent will equal the size of each datagram received by the receiving host. In addition, since UDP is a connectionless protocol, any datagrams sent to the host are not guaranteed to be delivered. Therefore, any required error checking (outside of UDP's checksum implementation) will have to be done by the application-layer protocol.

Example

The following example demonstrates creating a UDP echo server application which listens for datagrams and echoes them back to the sender.

[Visual Basic] 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
   ' Listen for datagrams on port 7.
   Udp1.Open(7)

   ' Begin an asynchronous Receive
   Dim buffer(Udp1.BufferSize) As Byte
   Udp1.BeginReceive(buffer)
End Sub

Private Sub Udp1_EndReceive(ByVal sender As Object, ByVal e As DatagramEventArgs) Handles Udp1.EndReceive

   ' Check for an Exception
   If e.Exception is Nothing Then
      ' Echo the data back using the Datagram object passed into the event.
      ' Datagram.Buffer = data received from client
      ' Datagram.RemoteEndPoint = address/port of client.
      Udp1.Send(e.Datagram.Buffer, e.Datagram.RemoteEndPoint)
   End If

   ' Start receiving next
   Dim buffer(Udp1.BufferSize) As Byte
   Udp1.BeginReceive(buffer)

End Sub

[C#] 


private void StartServer()
{            
  
// Listen for datagrams on port 7.
  
udp1.Open(7);

  
// Begin an asynchronous Receive
  
byte[] buffer = new byte[udp1.BufferSize];
  udp1.BeginReceive(buffer);
}

private void udp1_EndReceive(object sender, DatagramEventArgs e)
{
  
// Check for an exception
  
if(e.Exception == null)
  {
     
// Echo the data back using the Datagram object passed into the event.
     
// Datagram.Buffer = data received from client
     
// Datagram.RemoteEndPoint = address/port of client.
     
Datagram d = udp1.Send(e.Datagram.Buffer, e.Datagram.RemoteEndPoint);
  }        
  
// Start receiving next
  
byte[] buffer = new byte[udp1.BufferSize];
  udp1.BeginReceive(buffer);
}
                

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

Udp Class  | Udp Members  | Overload List


Send comments on this topic.

Documentation version 1.1.2.0.

© 2008 Dart Communications.  All rights reserved.